package com.efounder.view.imageaffixview;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.efounder.imageloader.GlideImageLoader;
import com.efounder.mobilemanager.R;
import com.efounder.view.ProgressView;

import java.io.File;
import java.util.List;



public class ViewAdapter<T> extends RecyclerView.Adapter<ViewAdapter.CustomViewHolder> {

    Context mContext;
    List<T> listDatas;

    private int imageWidth;

    private int imageHeight;

    //是否显示加号
    private boolean isShowAddButton = false;

    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onIconDeleteClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public ViewAdapter(Context context, List<T> datas) {
        this.mContext = context;
        this.listDatas = datas;
    }

    @Override
    public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        CustomViewHolder holder = new CustomViewHolder(LayoutInflater.from(mContext).inflate(R
                .layout.ef_dj_adpter_item, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewAdapter.CustomViewHolder holder, final int position) {
        //是否显示加号
        if (isShowAddButton) {
            if (position == listDatas.size()) {
//                holder.imgView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                holder.imgView.setImageResource(R.mipmap.icon_addpic_unfocused);
                holder.progressView.setVisibility(View.GONE);
                holder.progressBar.setVisibility(View.GONE);
                holder.imgUploadErrorView.setVisibility(View.GONE);
            } else {
                ImageModel data = (ImageModel) listDatas.get(position);
                String url = data.getUrl();
                if(url!=null&&!url.equals("")){
                    String localUrl = url.substring("file://".length(),url.length());
                    if (!new File(localUrl).exists()){
                        url = data.getUrl();
                    }
                }else{
                    url = data.getUrl();
                }
                if (url!=null&&!url.equals("")) {
                    GlideImageLoader.getInstance().displayImage(mContext,holder.imgView,url,R.mipmap.default_img);
//                    imageLoader.displayImage(url, holder.imgView, options);
                }else{
                    holder.imgView.setImageResource(R.mipmap.icon_addpic_error);
                }
                int progress= data.getProgress();
                if (progress == 100 /*|| progress == 0*/) {
                    holder.progressView.setVisibility(View.GONE);
                    holder.progressBar.setVisibility(View.GONE);
                } else {
                    holder.progressBar.setVisibility(View.VISIBLE);
//                    holder.progressView.setVisibility(View.VISIBLE);
//                    holder.progressView.setProgress(progress);
                }
                if(!data.isUploadSuccess()){
                    holder.progressBar.setVisibility(View.GONE);
                    holder.imgUploadErrorView.setVisibility(View.VISIBLE);
                }else{
                    holder.imgUploadErrorView.setVisibility(View.GONE);
                }
            }
        } else {
            ImageModel data = (ImageModel) listDatas.get(position);
            if (data.getUrl()!=null&&!data.getUrl().equals("")) {
                GlideImageLoader.getInstance().displayImage(mContext,holder.imgView,data.getUrl(),R.mipmap.default_img);
//                imageLoader.displayImage(data.getUrl(), holder.imgView, options);
            }else{
                holder.imgView.setImageResource(R.mipmap.icon_addpic_error);
            }
//            holder.progressView.setVisibility(View.GONE);
//            holder.progressBar.setVisibility(View.GONE);

            int progress= data.getProgress();
            if (progress == 100||progress == 0) {
                holder.progressView.setVisibility(View.GONE);
                holder.progressBar.setVisibility(View.GONE);
            } else {
                //holder.progressBar.setVisibility(View.VISIBLE);
                    holder.progressView.setVisibility(View.VISIBLE);
                    holder.progressView.setProgress(progress);
            }
            if(!data.isUploadSuccess()){
                holder.progressBar.setVisibility(View.GONE);
                holder.imgUploadErrorView.setVisibility(View.VISIBLE);
            }else{
                holder.imgUploadErrorView.setVisibility(View.GONE);
            }


        }
        if (null != onItemClickListener) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(holder.itemView, holder.getLayoutPosition());
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if (position<listDatas.size())
                    onItemClickListener.onItemLongClick(holder.itemView, holder.getLayoutPosition
                            ());
                    return true;
                }
            });
            holder.iconDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onIconDeleteClick(holder.itemView, holder.getLayoutPosition());
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        if (isShowAddButton)
            return listDatas.size() + 1;
        else
            return listDatas.size();
    }

    class CustomViewHolder extends RecyclerView.ViewHolder {
        ImageView imgView,iconDelete;
        ImageView imgUploadErrorView;
        ProgressView progressView;
        ProgressBar progressBar;

        public CustomViewHolder(View itemView) {
            super(itemView);
            imgView = (ImageView) itemView.findViewById(R.id.item_grida_image);
            iconDelete = (ImageView) itemView.findViewById(R.id.iv_icon_delete);
            imgUploadErrorView = (ImageView) itemView.findViewById(R.id.iv_icon_upload_error);
            progressView = (ProgressView) itemView.findViewById(R.id.progressView);
            progressBar = (ProgressBar) itemView.findViewById(R.id.pb);
//            FrameLayout imgView_container = (FrameLayout) itemView.findViewById(R.id
//                    .uploadimage_container);
//            ViewGroup.LayoutParams params = imgView_container.getLayoutParams();
//            params.height = getImageHeight();
//            params.width = getImageWidth();
//            imgView_container.setLayoutParams(params);

        }
    }

    public int getImageWidth() {
        return imageWidth;
    }

    public void setImageWidth(int imageWidth) {
        this.imageWidth = imageWidth;
    }

    public int getImageHeight() {
        return imageHeight;
    }

    public void setImageHeight(int imageHeight) {
        this.imageHeight = imageHeight;
    }

    public boolean isShowAddButton() {
        return isShowAddButton;
    }

    public void setShowAddButton(boolean showAddButton) {
        isShowAddButton = showAddButton;
    }

}